﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Archive.Data;
using CentralOffice.Archive.BusinessActions;

using CategoryTable = CentralOffice.Archive.Data.CategoryDataSet.CategoryDataTable;
using CategoryRow = CentralOffice.Archive.Data.CategoryDataSet.CategoryRow;

using ArchiveTable = CentralOffice.Archive.Data.ArchiveDataSet.ArchivesDataTable;
using ArchiveRow = CentralOffice.Archive.Data.ArchiveDataSet.ArchivesRow;
using System.Data;

namespace CentralOffice.WebClient.Archives
{
    public partial class ArchiveView : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                bool isAuthorized = this.Authorize(AuthorizationRules.Archive_View);

                if (!isAuthorized)
                {
                    lblJavaScript.Text = "<script type=\"text/javascript\">alert('您无权查看该档案！');window.location = 'ArchiveQuery.aspx';</script>";
                    return;

                }

                //string[] roles = System.Web.Security.Roles.GetRolesForUser();
                //if (roles.Contains("总经理"))
                //{
                //    //chkIsFinal.Checked = true;
                //}

                

                string id = HttpContext.Current.Request.QueryString["id"];
                ArchiveRow archive = CentralOffice.Archive.BusinessActions.Archive.GetById(new Guid(id));

                
                DataSet ds = null;

                if (!String.IsNullOrEmpty(archive.Description))
                {
                    ds = new DataSet();
                    System.IO.StringReader sw = new System.IO.StringReader(archive.Description);
                    ds.ReadXml(sw);
                }

                if (archive != null)
                {
                    #region 
                    lblCustomer.Text = archive.Customer;
                    //lblQueryNumber.Text = archive.QueryNumber;
                    lblAddress.Text = archive.Address;
                    lblTelephone1.Text = archive.Telephone1;
                    //lblTelephone2.Text = archive.Telephone2;
                    //lblFax.Text = archive.Fax;
                    //lblEmail.Text = archive.Email;
                    lblContact.Text = archive.Contact;
                    //lblContactTitle.Text = archive.ContactTitle;
                    //lblContactPhone.Text = archive.ContactPhone;


                    lblName.Text = archive.Name;
                    //lblQueryNumber.Text = archive.QueryNumber;
                    lblSecretLevel.Text = archive.SecretLevel;
                    lblProvince.Text = archive.Province;
                    lblCity.Text = archive.City;
                    //lblCounty.Text = archive.County;
                    lblContractCode.Text = archive.ContractCode;
                    lblFiledDate.Text = archive.FiledDate.ToString("yyyy-MM-dd");
                    lblKeywords.Text = archive.KeyWords;
                    lblDesignedYear.Text = archive.DesignedYear;
                    lblOrderNumber.Text = archive.OrderNumber.ToString();
                    lblFileNumber.Text = archive.QueryNumber;
                    //lblCustomerId.Text = archive.CustomerId;
                    lblFileCategory.Text = archive.Category;
                    if (!archive.IsScaleNull())
                    {
                        lblScale.Text = archive.Scale.ToString();
                    }
                    lblConstructForm.Text = archive.ConstructForm;
                    lblDesignStage.Text = archive.DesignStage;
                    lblBelongedTo.Text = archive.BelongedTo;
                    lblProjectManager.Text = archive.ProjectManager;

                    lblExecutor.Text = archive.IsExecutorNull() ? "" : archive.Executor;

                    if (!archive.IsBuildingBlueprintNull() && archive.BuildingBlueprint.Length > 0)
                    {
                        //lblBuildingBlueprint.Text = archive.BuildingBlueprint.Substring(archive.BuildingBlueprint.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblBuildingBlueprint.NavigateUrl = archive.BuildingBlueprint;
                        lblBuildingBlueprint.Visible = true;

                        SiteUtil.GenernateDownloadHyperLink(lblBuildingBlueprint, archive.BuildingBlueprint, !Permissive(archive, ds, "BuildingBlueprint"));
                    }


                    if (!archive.IsElectricalBlueprintNull() && archive.ElectricalBlueprint.Length > 0)
                    {
                        //lblElectricalBlueprint.Text = archive.ElectricalBlueprint.Substring(archive.ElectricalBlueprint.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblElectricalBlueprint.NavigateUrl = archive.ElectricalBlueprint;
                        lblElectricalBlueprint.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblElectricalBlueprint, archive.ElectricalBlueprint, !Permissive(archive, ds, "ElectricalBlueprint"));
                    }

                    if (!archive.IsMechanicalMaterialManifestNull() && archive.MechanicalMaterialManifest.Length > 0)
                    {
                        //lblMechanicalMaterialManifest.Text = archive.MechanicalMaterialManifest.Substring(archive.MechanicalMaterialManifest.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblMechanicalMaterialManifest.NavigateUrl = archive.MechanicalMaterialManifest;
                        lblMechanicalMaterialManifest.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblMechanicalMaterialManifest, archive.MechanicalMaterialManifest, !Permissive(archive, ds, "MechanicalMaterialManifest"));
                    }

                    if (!archive.IsElectricalMaterialManifestNull() && archive.ElectricalMaterialManifest.Length > 0)
                    {
                        //lblElectricalMaterialManifest.Text = archive.ElectricalMaterialManifest.Substring(archive.ElectricalMaterialManifest.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblElectricalMaterialManifest.NavigateUrl = archive.ElectricalMaterialManifest;
                        lblElectricalMaterialManifest.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblElectricalMaterialManifest, archive.ElectricalMaterialManifest, !Permissive(archive, ds, "ElectricalMaterialManifest"));
                    }

                    if (!archive.IsSystemSolutionNull() && archive.SystemSolution.Length > 0)
                    {
                        //lblSystemSolution.Text = archive.SystemSolution.Substring(archive.SystemSolution.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblSystemSolution.NavigateUrl = archive.SystemSolution;
                        lblSystemSolution.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblSystemSolution, archive.SystemSolution, !Permissive(archive, ds, "SystemSolution"));
                    }

                    if (!archive.IsElectricalSolutionNull() && archive.ElectricalSolution.Length > 0)
                    {
                        //lblElectricalSolution.Text = archive.ElectricalSolution.Substring(archive.ElectricalSolution.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblElectricalSolution.NavigateUrl = archive.ElectricalSolution;
                        lblElectricalSolution.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblElectricalSolution, archive.ElectricalSolution, !Permissive(archive, ds, "ElectricalSolution"));
                    }

                    if (!archive.IsDesignSketchNull() && archive.DesignSketch.Length > 0)
                    {
                        lblDesignSketch.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblDesignSketch, archive.DesignSketch, !Permissive(archive, ds, "DesignSketch"));
                    }

                    if (!archive.IsTenderDocumentationNull() && archive.TenderDocumentation.Length > 0)
                    {
                        lblTenderDocumentation.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblTenderDocumentation, archive.TenderDocumentation, !Permissive(archive, ds, "TenderDocumentation"));
                    }

                    if (!archive.IsPreliminaryDesignNull() && archive.PreliminaryDesign.Length > 0)
                    {
                        lblPreliminaryDesign.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblPreliminaryDesign, archive.PreliminaryDesign, !Permissive(archive, ds, "PreliminaryDesign"));
                    }

                    if (!archive.IsOtherDocumentationNull() && archive.OtherDocumentation.Length > 0)
                    {
                        lblOtherDocumentation.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblOtherDocumentation, archive.OtherDocumentation, !Permissive(archive, ds, "OtherDocumentation"));


                    }


                    #endregion
                    //
                    if (archive.CreatedBy == ApplicationContext.ActualUserId || this.Authorize(AuthorizationRules.Archive_AuthorityEdit))
                    {
                        divAuth.Visible = true;
                        linkAuthority.NavigateUrl = "ArchiveAuthority.aspx?id=" + archive.Id.ToString();
                    }
                }
            }

            
        }

        private bool Permissive(ArchiveRow archive, DataSet ds, string doc)
        {

            bool permissive = false;

            // 如果是本人浏览页面，没有限制
            if (archive.CreatedBy == ApplicationContext.ActualUserId)
            {
                return true;
            }
            // 如果没有进行访问限制，没有限制
            if (ds == null)
                return true;// permissive;

            permissive = true;
            DataTable dt = ds.Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                if (dr["Doc"].ToString() == doc)
                {
                    if ((dr["Users"] + ",").Contains(ApplicationContext.ActualUserName + ","))
                    {
                        permissive = true;
                    }
                    else
                    {
                        permissive = false;
                    }

                    break;
                }
            }

            return permissive;

        }
    }
}
